package org.cladwell.generaor;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.cladwell.model.MainTempalteCOnfig;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DynamicGenerator {
    public static void main(String[] args) throws IOException, TemplateException {
//        Configuration configuration = new Configuration(Configuration.VERSION_2_3_32);
//        // 指定模板文件所在的路径
////        String projiect=System.getProperty("user.dir")+File.separator+"generator-basic"+File.separator+"src"+File.separator+"main"
////        +File.separator+"resources"+File.separator+"template";
////        System.out.println(projiect);
////        configuration.setDirectoryForTemplateLoading(new File(projiect+"src/main/resources/template"));
//        String projectpath = System.getProperty("user.dir")+File.separator+"generator-basic";
//        File Parentfile=new File(projectpath);
//        File file1=new File(Parentfile,"src/main/resources/template");
//        configuration.setDirectoryForTemplateLoading(file1);
////         设置模板文件使用的字符集
//        configuration.setDefaultEncoding("utf-8");
//        Template template=configuration.getTemplate("MainTemplate.java.ftl");
//
//        MainTempalteCOnfig mainTempalteCOnfig=new MainTempalteCOnfig();
//        mainTempalteCOnfig.setAuthor("cladwellchen");
//        mainTempalteCOnfig.setOutputText("这是结果");
//        mainTempalteCOnfig.setLoop(true);
////        生成myweb.html
//        Writer out = new FileWriter("cladwelltest2.java");
//        template.process(mainTempalteCOnfig, out);
//
//        // 生成文件后别忘了关闭哦
//        out.close();
        String projectpath = System.getProperty("user.dir") + File.separator + "generator-basic";
        String inputfile = projectpath + File.separator + "src/main/resources/template/MainTemplate.java.ftl";
        String outpath = projectpath+File.separator + "cbh.java";
        MainTempalteCOnfig mainTempalteCOnfig = new MainTempalteCOnfig();
        mainTempalteCOnfig.setAuthor("cladwellchen");
        mainTempalteCOnfig.setOutputText("这是结果");
        mainTempalteCOnfig.setLoop(false);
      doGenerate(inputfile, outpath,mainTempalteCOnfig);
    }

    public static void doGenerate(String inputPath, String output, Object model) throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_32);
        // 指定模板文件所在的路径
//        String projiect=System.getProperty("user.dir")+File.separator+"generator-basic"+File.separator+"src"+File.separator+"main"
//        +File.separator+"resources"+File.separator+"template";
//        System.out.println(projiect);
//        configuration.setDirectoryForTemplateLoading(new File(projiect+"src/main/resources/template"));
//        String projectpath = System.getProperty("user.dir")+File.separator+"generator-basic";
        File Parentfile = new File(inputPath).getParentFile();

        configuration.setDirectoryForTemplateLoading(Parentfile);
//         设置模板文件使用的字符集
        configuration.setDefaultEncoding("utf-8");
        configuration.setNumberFormat("0.######");
        String templateName = new File(inputPath).getName();
        Template template = configuration.getTemplate(templateName);


//        生成myweb.html
        Writer out = new FileWriter(output);
        template.process(model, out);

        // 生成文件后别忘了关闭哦
        out.close();

    }
}
